package com.area.service.impl;

import com.area.entity.Area;
import com.area.entity.vo.AreaVo;
import com.area.mapper.AreaMapper;
import com.area.service.IAreaService;
import com.code.util.Variable;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.code.domain.PageParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * BASIC_地区 服务实现类
 * </p>
 *
 * @author guanfenga@yonyou.com
 * @since 2022-08-17
 */
@Service
@Transactional
@Slf4j
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {

    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 查询区块
     * @param area
     * @return
     */
    @Override
    public List<Area> queryAreaList(Area area) {
        LambdaQueryWrapper<Area> wrapper = new QueryWrapper<Area>().lambda();
         //wrapper.and(wrapper1 -> wrapper1.eq(Area::getAreaId,area.getAreaId()));
        return areaMapper.selectList(wrapper);
    }

    /**
     * 分页查询
     * @param pageParam
     * @param area
     * @return
     */
    @Override
    public IPage<Area> queryAreaPage(PageParam<Area> pageParam, Area area) {

        Page listPage = new Page(1,10);
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        if(area != null){
            if(area.getFullName() != null){
                queryWrapper.like("fullName",area.getFullName());
            }
        }
        queryWrapper.orderByDesc("areaId");

        return areaMapper.selectPage(listPage,queryWrapper);
    }
    /**
     * 新增区块
     * @param area
     * @return
     */
    @Override
    public int saveArea(Area area) {
        return areaMapper.insert(area);
    }

    @Override
    public int updateArea(Area area) {
        return areaMapper.updateById(area);
    }

    @Override
    public List<Map<String,Object>> findAreaPage(Integer pageBefore, Integer limit) {
        return areaMapper.findAreaPage(pageBefore,limit);
    }

    @Override
    public List<Area> findAreaPage(Integer page, Integer limit, Area area) {
        return areaMapper.findAreaPage(page,limit ,area);
    }

    @Override
    public boolean getAreaToRedis() {
        //设置标志位
        boolean result = true;
        //查询所有区划信息
        LambdaQueryWrapper<Area> wrapper = new QueryWrapper<Area>().lambda();
        //将查询的结果同步到redis 中
        redisTemplate.opsForValue().set(Variable.AREA_LIST,areaMapper.selectList(wrapper));

        return result;
    }

    @Override
    public List<Area> queryAreaLayUiRedisPage(AreaVo areaVo) {
        //查询redis 中是否存在区划缓存
        List<Area> areaList = (List<Area>) redisTemplate.opsForValue().get(Variable.AREA_LIST);

        return areaList;
    }

    @Override
    public List<Area> queryBasicAreaAll() {
        //查询redis 缓存中是否存在，如果不存在则进行数据库的查询
        List<Area> areaList = (List<Area>) redisTemplate.opsForValue().get(Variable.AREA_LIST);
        if(areaList == null){
            areaList = areaMapper.queryBasicAreaAll();
        }
        return areaList;
    }


}
